package com.zhonghui.scm.controller;

import com.zhonghui.common.annotation.Log;
import com.zhonghui.common.core.controller.BaseController;
import com.zhonghui.common.core.page.TableDataInfo;
import com.zhonghui.common.enums.BusinessType;
import com.zhonghui.common.utils.DateUtils;
import com.zhonghui.common.utils.SecurityUtils;
import com.zhonghui.common.utils.poi.ExcelUtil;
import com.zhonghui.response.BaseResult;
import com.zhonghui.scm.domain.ScmSaleContract;
import com.zhonghui.scm.service.IScmSaleContractService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 销售合同Controller
 *
 * @author zhonghui
 * @date 2022-05-26
 */
@Api(tags="供应链SCM-销售管理-销售合同")
@RestController
@RequestMapping("/scm/contract")
public class ScmSaleContractController extends BaseController
{
    @Autowired
    private IScmSaleContractService scmSaleContractService;

    /**
     * 查询销售合同列表
     */
    @ApiOperation("销售管理-销售合同列表")
    @GetMapping("/list")
    public TableDataInfo<List<ScmSaleContract>> getList(ScmSaleContract scmSaleContract)
    {
        startPage();
        List<ScmSaleContract> list = scmSaleContractService.getList(scmSaleContract);
        return getDataTable(list);
    }

    /**
     * 导出销售合同列表
     */
    @ApiOperation("导出销售管理-销售合同列表")
    @PostMapping("/export")
    public void export(HttpServletResponse response, ScmSaleContract scmSaleContract)
    {
        List<ScmSaleContract> list = scmSaleContractService.getList(scmSaleContract);
        ExcelUtil<ScmSaleContract> util = new ExcelUtil<ScmSaleContract>(ScmSaleContract.class);
        util.exportExcel(response, list, "销售合同数据");
    }

    /**
     * 获取销售合同详细信息
     */
    @ApiOperation("销售管理-销售合同详细信息")
    @GetMapping(value = "/{id}")
    public BaseResult<ScmSaleContract> getById(@PathVariable("id") Long id)
    {
        return BaseResult.success(scmSaleContractService.getById(id));
    }

    /**
     * 新增销售合同
     */
    @ApiOperation("新增销售管理-销售合同")
    @PostMapping
    public BaseResult<Integer> add(@RequestBody ScmSaleContract scmSaleContract)
    {
        // 申请状态 0未提交 1待审核 2已审核
        scmSaleContract.setApplyStatus(0);
        return BaseResult.success(scmSaleContractService.add(scmSaleContract));
    }

    /**
     * 修改销售合同
     */
    @ApiOperation("修改销售管理-销售合同")
    @PutMapping
    public BaseResult<Integer> edit(@RequestBody ScmSaleContract scmSaleContract)
    {
        scmSaleContract.setApplyStatus(0);
        scmSaleContract.setAuditorId(null);
        scmSaleContract.setAuditStatus(null);
        scmSaleContract.setAuditTime(null);
        scmSaleContract.setAuditorComments("");
        return BaseResult.success(scmSaleContractService.edit(scmSaleContract));
    }
    @ApiOperation("提交销售合同申请")
    @Log(title = "提交销售合同申请", businessType = BusinessType.UPDATE)
    @PutMapping("/submit")
    public BaseResult<Integer> submit(@RequestBody ScmSaleContract scmSaleContract) {
        scmSaleContract.setApplyStatus(1);
        scmSaleContract.setApplyDate(DateUtils.getNowDate());
        if(scmSaleContract.getId() != null) {
            // 修改提交
            ScmSaleContract saleContract = scmSaleContractService.getById(scmSaleContract.getId());
            if(saleContract == null) {
                return BaseResult.error("此销售合同不存在，不能提交！");
            }
            scmSaleContract.setAuditorId(null);
            scmSaleContract.setAuditStatus(null);
            scmSaleContract.setAuditTime(null);
            scmSaleContract.setAuditorComments("");
            return BaseResult.success(scmSaleContractService.edit(scmSaleContract));
        }else {
            // 新增提交
            return BaseResult.success(scmSaleContractService.add(scmSaleContract));
        }
    }

    @ApiOperation("审核销售合同")
    @Log(title = "审核销售合同", businessType = BusinessType.UPDATE)
    @PutMapping("/approve")
    public BaseResult<Integer> approve(@RequestBody ScmSaleContract scmSaleContract) {
        scmSaleContract.setApplyStatus(2);
        scmSaleContract.setAuditTime(DateUtils.getNowDate());
        scmSaleContract.setAuditorId(SecurityUtils.getLoginUser().getUser().getUserId());
        return BaseResult.success(scmSaleContractService.edit(scmSaleContract));
    }
    /**
     * 删除销售合同
     */
    @ApiOperation("删除销售管理-销售合同")
	@DeleteMapping("/{ids}")
    public BaseResult<Integer> remove(@PathVariable Long[] ids)
    {
        return BaseResult.success(scmSaleContractService.del(ids));
    }
}
